home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Nebula 1
/
Nebula One.iso
/
Utilities
/
Scanner
/
Scan.0.91
/
Source
/
scanView.m
< prev
Wrap
Text File
|
1996-02-03
|
7KB
|
281 lines
#import "scanView.h"
@implementation scanView
- initFrame:(const NXRect *)frameRect
{
[super initFrame:frameRect];
[self setMyTrackingRect: YES]; // to enable mouse tracking
scalex = 1200.0/72.0;
scaley = 1200.0/72.0;
oldposition.x = -1.0;
// size for 8.5 x 11 page, at 1200 dpi, scaled to 72 dpi.
[self setDrawSize:10200/scalex :14040/scaley];
bitmapForView = 0x0;
[self clearDisplay:self];
[[self window] makeKeyAndOrderFront:self];
return self;
}
- awakeFromNib
{
[[self window] makeKeyAndOrderFront:self];
return self;
}
- drawSelf:(const NXRect *)rects :(int)rectCount
{
// max x-size = 8.5" x 1200 dpi = 10200 pixels.
// max y-size = 11.7" x 1200 dpi = 14040 pixels.
if (clearFlag)
{
PSsetgray(NX_WHITE);
PSrectfill(bounds.origin.x, bounds.origin.y,
bounds.size.width, bounds.size.height);
}
if (bitmapForView && !clearFlag)
{
[bitmapForView drawAt:&bitmapPos];
}
clearFlag = 0;
return self;
}
- drawBitmap:(NXBitmapImageRep *)bitmap :(int)xstart :(int)ystart
{
NXSize imageSize;
NXPoint start, finish;
bitmapForView = bitmap;
bitmapPos.x = xstart;
bitmapPos.y = ystart;
if (oldposition.x >= 0)
{
start.x = [startx intValue]/scalex;
start.y = (14040 - [starty intValue])/scaley;
finish.x = start.x + [width intValue]/scalex;
finish.y = start.y - [height intValue]/scaley;
[self doMyOwnUnhighlight:start :finish];
}
[bitmapForView getSize:&imageSize];
// correct pixel coordinates for origin.
bitmapPos.x = bitmapPos.x/scalex;
bitmapPos.y = (14040 - bitmapPos.y)/scaley - imageSize.height;
[self display];
return self;
}
- clearDisplay:sender
{
clearFlag = 1;
[self display];
return self;
}
- setMyTrackingRect:(BOOL)flag
{
NXRect visible;
/* discard old tracking rect if present */
if (trackingRect) {
[window discardTrackingRect:trackingRect];
trackingRect = 0;
}
if (flag) {
/* set new tracking rect if requested and if visible */
if ([self getVisibleRect:&visible]) {
[self convertRect:&visible toView:nil];
[window setTrackingRect:&visible
inside:NO owner:self
tag:1 left:NO right:NO];
trackingRect = 1;
}
}
return self;
}
- mouseEntered:(NXEvent *)theEvent
{
[window makeFirstResponder:self];
inside = YES;
return self;
}
- mouseExited:(NXEvent *)theEvent
{
inside = NO;
return self;
}
- mouseDown:(NXEvent *)thisEvent
/* modified from the online docs */
{
int shouldLoop = YES;
int oldMask;
NXEvent *nextEvent;
NXPoint position;
NXPoint newposition;
float maxX = 10200/scalex;
float maxY = 14040/scaley;
if (oldposition.x >= 0)
{
position.x = [startx intValue]/scalex;
position.y = (14040 - [starty intValue])/scaley;
newposition.x = position.x + [width intValue]/scalex;
newposition.y = position.y - [height intValue]/scaley;
[self doMyOwnUnhighlight:newposition :position];
}
position = thisEvent->location;
[self convertPoint:&position fromView:nil];
if (position.x < 0) position.x = 0;
if (position.x > maxX) position.x = maxX;
if (position.y < 0) position.y = 0;
if (position.y > maxY) position.y = maxY;
[startx setIntValue:position.x*scalex];
[starty setIntValue:14040 - position.y*scaley];
[width setIntValue:0];
[height setIntValue:0];
oldMask = [window addToEventMask:NX_LMOUSEDRAGGEDMASK];
while (shouldLoop) {
nextEvent = [NXApp getNextEvent:(NX_LMOUSEUPMASK |
NX_LMOUSEDRAGGEDMASK)];
// get the position of the event in screen coordinates and
// convert to our particular view coordinates.
newposition = nextEvent->location;
[self convertPoint:&newposition fromView:nil];
if (newposition.x < 0) newposition.x = 0;
else if (newposition.x > maxX) newposition.x = maxX;
if (newposition.y < 0) newposition.y = 0;
else if (newposition.y > maxY) newposition.y = maxY;
if (newposition.x < position.x)
{
[startx setIntValue:newposition.x*scalex];
[width setIntValue:(position.x - newposition.x)*scalex];
}
else
{
[startx setIntValue:position.x*scalex];
[width setIntValue:(newposition.x - position.x)*scalex];
}
if (newposition.y > position.y)
{
[starty setIntValue:14040 - newposition.y*scaley];
[height setIntValue:(newposition.y - position.y)*scaley];
}
else
{
[starty setIntValue:14040 - position.y*scaley];
[height setIntValue:(position.y - newposition.y)*scaley];
}
// We are interested in only 2 types of events.
switch (nextEvent->type) {
case NX_LMOUSEUP:
shouldLoop = NO;
if (position.x != newposition.x) {
[self doMyOwnHighlight:newposition :position];
}
// [self doMyOwnUnhighlight:newposition :position];
break;
case NX_LMOUSEDRAGGED:
[self doMyOwnHighlight:newposition :position];
break;
default:
break;
}
}
[window setEventMask:oldMask];
return(self);
}
- doMyOwnHighlight:(NXPoint)newposition :(NXPoint)position
{
BOOL erase = YES;
// if we are where we started from, do nothing.
if (newposition.x == oldposition.x
&& newposition.y == oldposition.y ) return self;
if (oldposition.x < 0) // first time through,
{
erase = NO; // don't do an erase.
oldposition.x = newposition.x; // last position = current pos.
oldposition.y = newposition.y;
}
[self lockFocus];
// erase the old highlight (if there is one)
if (erase) PScompositerect(floor(position.x), floor(position.y),
floor(oldposition.x - position.x),
floor(oldposition.y - position.y), NX_HIGHLIGHT);
// draw the new highlight
PScompositerect(floor(position.x), floor(position.y),
floor(newposition.x - position.x),
floor(newposition.y - position.y), NX_HIGHLIGHT);
[self unlockFocus];
[window flushWindow];
oldposition.x = newposition.x;
oldposition.y = newposition.y;
return self;
}
- doMyOwnUnhighlight:(NXPoint)newposition :(NXPoint)position
{
[self lockFocus];
// erase the old highlight (if there is one)
PScompositerect(floor(position.x), floor(position.y),
floor(newposition.x - position.x),
floor(newposition.y - position.y), NX_HIGHLIGHT);
[self unlockFocus];
[window flushWindow];
oldposition.x = -1.0; // so next time through, start fresh.
return self;
}
- windowDidBecomeMain:sender
{
return [self setMyTrackingRect: YES];
}
- windowDidResignMain:sender
{
return [self setMyTrackingRect: NO];
}
@end